package com.chinawayltd.ews.sample.service.main.impl;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chinawayltd.ews.sample.mapper.main.MainExtendMapper;
import com.chinawayltd.ews.sample.service.main.MainService;
import com.chinawayltd.ews.sample.util.StringUtils;

/**
 * @program: greeProject
 * @description 首页和监控大屏
 * @author: chenxin
 * @create: 2018年5月4日 上午11:24:34
 */
@Service
public class MainServiceImpl implements MainService {
	private static final SimpleDateFormat YYYYMMDD = new SimpleDateFormat("yyyy-MM-dd");
	private static final SimpleDateFormat MMDD = new SimpleDateFormat("MM/dd");
	@Autowired
	private MainExtendMapper mainExtendMapper;
	
	public Map<String, Object> getVehicleDataByDay() throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		//查询一个月的金额数据
		Date date = new Date();
		String endTime = YYYYMMDD.format(date);
		Calendar calendar  = new GregorianCalendar();
		calendar.setTime(date);
		//calendar.add(Calendar.DATE, 1);
		endTime = YYYYMMDD.format(calendar.getTime());
		calendar.add(Calendar.DATE, -7);
		String beginTime = YYYYMMDD.format(calendar.getTime());
		//车辆使用率、总里程、总油耗
		List<Map<String, Object>> vehicleList = mainExtendMapper.getVehicleDataByDay(beginTime, endTime);
		//每日任务数
		List<Map<String, Object>> getTaskList = mainExtendMapper.getTaskDataByEveryDay(beginTime+" 00:00:01", endTime+" 00:00:00");
		//每日异常任务数
		List<Map<String, Object>> getTaskUnusualList = mainExtendMapper.getTaskUnusualDataEveryDay(beginTime+" 00:00:01", endTime+" 00:00:00");
		//每日执行任务的车辆数
		List<Map<String, Object>> vehicleNumList = new ArrayList<>();
		Calendar calendar2  = new GregorianCalendar();
		calendar2.setTime(date);
		calendar2.add(Calendar.DATE, -1);
		String mmddDate = "";
		for (int i = 0; i <8; i++) {
			String beginTime2 = YYYYMMDD.format(calendar2.getTime());
			mmddDate = MMDD.format(calendar2.getTime());
			calendar2.add(Calendar.DATE, 1);
			String endTime2 = YYYYMMDD.format(calendar2.getTime());
			Map<String, Object> getvehicleNumMap = mainExtendMapper.getVehicleNumByDay(beginTime2+" 00:00:01", endTime2+" 00:00:00");
			if(getvehicleNumMap == null){
				getvehicleNumMap = new HashMap<String, Object>();
				getvehicleNumMap.put("VEHICLE_NUM", 0);
				getvehicleNumMap.put("STAT_DATE", mmddDate);
			}
			vehicleNumList.add(getvehicleNumMap);
			calendar2.add(Calendar.DATE, -2);
		}
		// 待优化 TODO
		List<Map<String, Object>> taskNumList = new ArrayList<>();
		List<Map<String, Object>> mileageList = new ArrayList<>();
		List<Map<String, Object>> fuelList = new ArrayList<>();
		List<Map<String, Object>> ratioList = new ArrayList<>();
		Calendar calendars = new GregorianCalendar();
		String strDate=null;
		Map<String, Object> taskMap=null;
		Map<String, Object> mileageMap=null;
		Map<String, Object> fuelMap=null;
		Map<String, Object> ratioMap=null;
		DecimalFormat df=new DecimalFormat("0.0");
		Date resultDate=null;
		for (int i = 7; i > 0; i--) {
			mileageMap = new HashMap<>();
			fuelMap = new HashMap<>();
			ratioMap = new HashMap<>();
			taskMap = new HashMap<>();
			calendars.setTime(date);
			calendars.add(Calendar.DATE,-i);
			resultDate=calendars.getTime();
			strDate = MMDD.format(resultDate);
			mileageMap.put("DATE",strDate);
			fuelMap.put("DATE",strDate);
			ratioMap.put("DATE",strDate);
			mileageMap.put("VALUE",0 );
			fuelMap.put("VALUE",0);
			ratioMap.put("VALUE",0);
			taskMap.put("DATE",strDate);
			taskMap.put("TASK_VALUE",0);
			taskMap.put("TASK_UNUSUAL_VALUE",0);
			taskMap.put("VEHICLE_AVG_NUM",0);
			for (int j = 0; j < vehicleList.size(); j++) {
				String dateStr_j = org.apache.commons.collections4.MapUtils.getString(vehicleList.get(j), "STAT_DATE");
				String mileageStr_j = org.apache.commons.collections4.MapUtils.getString(vehicleList.get(j), "TOTAL_MILEAGE");
				String fuelStr_j = org.apache.commons.collections4.MapUtils.getString(vehicleList.get(j), "TOTAL_FUEL");
				String ratioStr_j = org.apache.commons.collections4.MapUtils.getString(vehicleList.get(j), "UTILIZATION_RATIO");
				if(dateStr_j.equals(strDate)){
					mileageMap.put("DATE",strDate);
					fuelMap.put("DATE",strDate);
					ratioMap.put("DATE",strDate);
					if(StringUtils.isNotBlank(mileageStr_j)){
						mileageMap.put("VALUE",mileageStr_j);
					}
					if(StringUtils.isNotBlank(fuelStr_j)){
						fuelMap.put("VALUE",fuelStr_j);			
					}
					if(StringUtils.isNotBlank(ratioStr_j)){
						ratioMap.put("VALUE",ratioStr_j);
					}
				}
			}
			for (int k = 0; k < getTaskList.size(); k++) {
				String dateStr_k = org.apache.commons.collections4.MapUtils.getString(getTaskList.get(k), "STAT_DATE");
				String taskNumStr_k = org.apache.commons.collections4.MapUtils.getString(getTaskList.get(k), "TASK_NUM");
				if(dateStr_k.equals(strDate)){
					taskMap.put("DATE",strDate);
					if(StringUtils.isNotBlank(taskNumStr_k)){
						taskMap.put("TASK_VALUE",taskNumStr_k);
					}
				}
			}
			for (int l = 0; l < getTaskUnusualList.size(); l++) {
				String dateStr_l = org.apache.commons.collections4.MapUtils.getString(getTaskUnusualList.get(l), "STAT_DATE");
				String taskUnusualNumStr_l = org.apache.commons.collections4.MapUtils.getString(getTaskUnusualList.get(l), "TASK_UNUSUAL_NUM");
				if(dateStr_l.equals(strDate)){
					taskMap.put("DATE",strDate);
					if(StringUtils.isNotBlank(taskUnusualNumStr_l)){
						taskMap.put("TASK_UNUSUAL_VALUE",taskUnusualNumStr_l);
					}
				}
			}
			for (int j = 0; j < vehicleNumList.size(); j++) {
				String dateStr_j = org.apache.commons.collections4.MapUtils.getString(vehicleNumList.get(j), "STAT_DATE");
				int vehicleNumStr_j = org.apache.commons.collections4.MapUtils.getIntValue(vehicleNumList.get(j), "VEHICLE_NUM");
				if(dateStr_j.equals(strDate)){
					taskMap.put("DATE",strDate);
					int object = org.apache.commons.collections4.MapUtils.getIntValue(taskMap, "TASK_VALUE");
					if(object ==0){
						taskMap.put("VEHICLE_AVG_NUM",0);
					}else{
						if(vehicleNumStr_j ==0){
							taskMap.put("VEHICLE_AVG_NUM",0);
						}else{
							String format = df.format((float)object/(float)vehicleNumStr_j);
							taskMap.put("VEHICLE_AVG_NUM",format);
						}
					}
				}
			}
			taskNumList.add(taskMap);
			mileageList.add(mileageMap);
			fuelList.add(fuelMap);
			ratioList.add(ratioMap);
		}
		
		resultMap.put("TOTAL_TASKNUM", taskNumList != null ? taskNumList : new ArrayList<Map<String, Object>>());
		resultMap.put("TOTAL_MILEAGE", mileageList != null ? mileageList : new ArrayList<Map<String, Object>>());
		resultMap.put("TOTAL_FUEL", fuelList != null ? fuelList : new ArrayList<Map<String, Object>>());
		resultMap.put("UTILIZATION_RATIO", ratioList != null ? ratioList : new ArrayList<Map<String, Object>>());
		return resultMap;
	}
	
	@Override
	public Map<String, Object> getTaskDataByDay() throws Exception {
		Map<String, Object> map = mainExtendMapper.getTaskDataByDay();
		Map<String, Object> taskUnusualDataByDay = this.getTaskUnusualDataByDay();
		map.put("TASK_UNUSUAL_NUM", taskUnusualDataByDay.get("TASK_UNUSUAL_NUM"));
		return map;
	}
	@Override
	public Map<String, Object> getTaskUnusualDataByDay() throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date endDate = new Date();
		String endTime = sdf.format(endDate);
		Calendar calendar  = new GregorianCalendar();
		calendar.setTime(endDate);
		calendar.add(Calendar.DATE, -1);
		String beginTime = sdf.format(calendar.getTime());
		Map<String, Object> list = mainExtendMapper.getTaskUnusualDataByDay(beginTime, endTime);
		return list;
	}

	@Override
	public Map<String, Object> getVehicleDataForDay(){
		Map<String, Object> resultMap = new HashMap<>();
		Date date = new Date();
		List<Map<String, Object>> taskNumList = new ArrayList<>();
		List<Map<String, Object>> mileageList = new ArrayList<>();
		List<Map<String, Object>> fuelList = new ArrayList<>();
		List<Map<String, Object>> ratioList = new ArrayList<>();
		Calendar calendars = new GregorianCalendar();
		String strDate=null;
		Map<String, Object> taskMap=null;
		Map<String, Object> mileageMap=null;
		Map<String, Object> fuelMap=null;
		Map<String, Object> ratioMap=null;
		Date resultDate=null;
		for (int i = 7; i > 0; i--) {
			mileageMap = new HashMap<>();
			fuelMap = new HashMap<>();
			ratioMap = new HashMap<>();
			taskMap = new HashMap<>();
			calendars.setTime(date);
			calendars.add(Calendar.DATE,-i);
			resultDate=calendars.getTime();
			strDate = MMDD.format(resultDate);
			mileageMap.put("DATE",strDate);
			fuelMap.put("DATE",strDate);
			ratioMap.put("DATE",strDate);
			mileageMap.put("VALUE",0 );
			fuelMap.put("VALUE",0);
			ratioMap.put("VALUE",0);
			taskMap.put("DATE",strDate);
			taskMap.put("TASK_VALUE",0);
			taskMap.put("TASK_UNUSUAL_VALUE",0);
			taskMap.put("VEHICLE_AVG_NUM",0);
			taskNumList.add(taskMap);
			mileageList.add(mileageMap);
			fuelList.add(fuelMap);
			ratioList.add(ratioMap);
		}
		
		resultMap.put("TOTAL_TASKNUM", taskNumList != null ? taskNumList : new ArrayList<Map<String, Object>>());
		resultMap.put("TOTAL_MILEAGE", mileageList != null ? mileageList : new ArrayList<Map<String, Object>>());
		resultMap.put("TOTAL_FUEL", fuelList != null ? fuelList : new ArrayList<Map<String, Object>>());
		resultMap.put("UTILIZATION_RATIO", ratioList != null ? ratioList : new ArrayList<Map<String, Object>>());
		return resultMap;
	}
	
}
